
package controlpack;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import modelpack.Buku;
import modelpack.Penjualan;
import modelpack.PenjualanDetail;


public class TokoService implements TokoServiceInterface {
    
	private Connection con;
	
	/*--------------------------------------------------------------------*/
    private static final String sqlInsert = 
            "insert into buku (kode, judul, pengarang, penerbit, harga)"+
            "values(?,?,?,?,?)";
    private static final String sqlDelete = "delete from buku where id=?";
    private static final String cariID = "select * from buku where id=?";
    private static final String cariKode = "select * from buku where kode like ?";
    private PreparedStatement psInsert;
    private PreparedStatement psCariId;
    private PreparedStatement psCariKode;
    private PreparedStatement psDelete;
    
    /*--------------------------------------------------------------------*/
    private static final String sqlInsertPenjualan = 
    	"insert into penjualan(waktu, kasir, pembayaran)"+
    	"values(?,?,?)";
    private PreparedStatement psInsertPenjualan;
    
    /*--------------------------------------------------------------------*/
    private static final String sqlLastInsert = "select last_insert_id()";
    private static final String sqlInsertPenjualanDetail = 
    	"insert into penjualan_detail(id_penjualan, id_buku, quantity)"+
    	"values(?,?,?)";
    private PreparedStatement psInsertPenjualanDetail;
    
    /*--------------------------------------------------------------------*/
    public Connection getCon() {
		return con;
	}

	public TokoService(Connection con) throws SQLException {
		this.con = con;
		psInsert = con.prepareStatement(sqlInsert);
		psCariId = con.prepareStatement(cariID);
		psCariKode = con.prepareStatement(cariKode);
		psDelete = con.prepareStatement(sqlDelete);
		psInsertPenjualan = con.prepareStatement(sqlInsertPenjualan);
		psInsertPenjualanDetail = con.prepareStatement(sqlInsertPenjualanDetail);
		
    }

    @Override
    public Buku cariKode(String kode) throws SQLException {
		Buku buku = new Buku();
		psCariKode.setString(1, "%"+kode+"%");
		ResultSet rs = psCariKode.executeQuery();
		while(rs.next()) {
	            buku.setId(rs.getInt("id"));
	            buku.setKode(rs.getString("kode"));
	            buku.setJudul(rs.getString("judul"));
	            buku.setPengarang(rs.getString("pengarang"));
	            buku.setPenerbit(rs.getString("penerbit"));
	            buku.setHarga(rs.getBigDecimal("harga"));
		}
		rs.close();
        return buku;
    }

	@Override
	    public Buku cariId(Integer id) throws SQLException {
		Buku buku = new Buku();
		psCariId.setInt(1, id);
		ResultSet rs = psCariId.executeQuery();
		while(rs.next()) {
            buku.setId(rs.getInt("id"));
            buku.setKode(rs.getString("kode"));
            buku.setJudul(rs.getString("judul"));
            buku.setPengarang(rs.getString("pengarang"));
            buku.setPenerbit(rs.getString("penerbit"));
            buku.setHarga(rs.getBigDecimal("harga"));
	}
		rs.close();
		return buku;
    }

    @Override
    public void simpan(Buku buku) {
    	try {
            psInsert.setString(1, buku.getKode());
            psInsert.setString(2, buku.getJudul());
            psInsert.setString(3, buku.getPengarang());
            psInsert.setString(4, buku.getPenerbit());
            psInsert.setBigDecimal(5, buku.getHarga());
            psInsert.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void hapus(Buku buku) {
        System.out.println("hapus.");
        try {
            psDelete.setInt(1, buku.getId());
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

	public void simpan2(Penjualan p) {
		try {
			System.out.println("coba masukin penjualan & detailnya.");
			con.setAutoCommit(false);// kalo ada yg eror, semua ga dimasukin.
			
			psInsertPenjualan.setDate(1, new Date(p.getWaktu().getTime()));
			psInsertPenjualan.setString(2, p.getKasir());
            psInsertPenjualan.setBigDecimal(3, p.getPembayaran());
            psInsertPenjualan.executeUpdate();
            
            ResultSet rs = con.createStatement().executeQuery(sqlLastInsert);
            rs.next();
            Integer aidi = rs.getInt(1);
           
            for (PenjualanDetail penjualanDetail : p.getDaftarPenjualanDetail()) {
				psInsertPenjualanDetail.setInt(1, aidi);
				psInsertPenjualanDetail.setInt(2, penjualanDetail.getBuku().getId());
				psInsertPenjualanDetail.setInt(3, penjualanDetail.getQuantity());
	            psInsertPenjualanDetail.executeUpdate();
			}
            
            con.commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            try {
				con.rollback();
			} catch (Exception e8) {
				e8.printStackTrace();
			}
				
        }
	}

}
